package com.xms.logging.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xms.logging.api.model.EntityLogListResult;
import com.xms.logging.entity.EntityLog;
import com.xms.logging.service.IEntityLogService;
import com.xms.schema.service.attribute.IAttributeFinderService;
import com.xms.utils.UUIDUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author migo
 * @since 2021-08-29
 */
@RestController
@RequestMapping("/{org}/api/log/entitylog")
public class EntitylogController extends ApiBaseController {
    private IEntityLogService _entityLogService;
    private IAttributeFinderService _attributeFinderService;
    @Autowired
    public EntitylogController(IEntityLogService entityLogService,
                               IAttributeFinderService attributeFinderService){
        _entityLogService = entityLogService;
        _attributeFinderService = attributeFinderService;
    }

    @ApiOperation("查询数据日志列表")
    @GetMapping("list")
    public JsonResultObject get(@RequestParam(value="operationtype", required = false) Integer operationtype,
                                @RequestParam(value="entityid", required = true) UUID entityid,
                                @RequestParam(value="getall", required = false) Boolean getall,
                                @RequestParam(value="page", required = false) Integer page,
                                @RequestParam(value="pagesize", required = false) Integer pagesize,
                                @RequestParam(value="sortby", required = false) String sortby,
                                @RequestParam(value="sortdirection", required = false) String sortdirection)
    {
        if(UUIDUtil.isEmpty(entityid)){
            return JNotSpecifiedParams("entityid");
        }
        LambdaQueryWrapper<EntityLog> lambdaQueryWrapper = getSortedQueryWrapper(sortby, sortdirection);
        if (operationtype != null)
        {
            lambdaQueryWrapper.eq(EntityLog::getOperationtype, operationtype);
        }
        if(UUIDUtil.notEmpty(entityid)){
            lambdaQueryWrapper.eq(EntityLog::getEntityid, entityid);
        }
        if (getall != null && getall) {
            return JOk(_entityLogService.list(lambdaQueryWrapper));
        }
        else if(page == null || pagesize == null){
            page = 1;
            pagesize = 10;
        }
        page = (page <= 0 ? 1 : page);
        pagesize = (pagesize <= 0 ? 10 : pagesize);
        IPage<EntityLog> datas = _entityLogService.page(new Page<>(page, pagesize), lambdaQueryWrapper);
        EntityLogListResult result = new EntityLogListResult();
        result.items = datas.getRecords();
        result.page = page;
        result.pagesize = pagesize;
        result.totalitems = datas.getTotal();
        result.totalpages = datas.getPages();
        result.attributes = _attributeFinderService.getByEntityId(entityid);
        return JOk(result);
    }

    @ApiOperation("清空指定实体的数据日志")
    @GetMapping("clearbyentity/{entityid}")
    public JsonResultObject clearbyentity(@PathVariable(value="entityid", required = true) UUID entityid){
        _entityLogService.clear(entityid);
        return JOk();
    }

    @ApiOperation("清空所有数据日志")
    @GetMapping("clearall")
    public JsonResultObject clearall(){
        _entityLogService.clear();
        return JOk();
    }
}
